import BaseLabel_blxf from "../Common/BaseLabel";

let wx = window['wx'];

const {ccclass, property} = cc._decorator;

/** 排行榜item */

@ccclass
export default class ShopRankItem_blxf extends cc.Component {

    @property({ type: cc.Sprite, tooltip: '名次图标' })
    rankIconSpr: cc.Sprite = null;

    @property({ type: cc.Node, tooltip: '名次' })
    rankNumNode: cc.Node = null;

    @property({ type: cc.Sprite, tooltip: '头像' })
    headimgSpr: cc.Sprite = null;;

    @property({ type: cc.Node, tooltip: '名称' })
    nameNode: cc.Node = null;

    @property({ type: cc.Node, tooltip: '商品数' })
    numNode: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {

    }

    // update (dt) {}

    initData_blxf ( data: any ) {
        let { KVDataList, rankNum, nickname, avatarUrl, imgAtlas } = data;
        this.rankNumNode.getComponent(BaseLabel_blxf).setText_blxf(rankNum);
        this.nameNode.getComponent(BaseLabel_blxf).setText_blxf( nickname );
        this.numNode.getComponent(BaseLabel_blxf).setText_blxf( KVDataList[0].value );

        let _isRankIcon: boolean = rankNum <= 3;
        this.rankNumNode.active = !_isRankIcon;
        this.rankIconSpr.node.active = _isRankIcon;
        _isRankIcon && this.updateRankIcon_blxf( rankNum, imgAtlas );

        this.updateHeadimg_blxf( avatarUrl );
    }

    /**
     * 更新前三名次图标
     * @param rankNum 排名
     */
    updateRankIcon_blxf ( rankNum: number, imgAtlas: cc.SpriteAtlas ) {
        let _imgName: string = `Jhb_Elt_${rankNum}`;
        this.rankIconSpr.spriteFrame = imgAtlas.getSpriteFrame( _imgName );
    }

    /**
     * 更新头像
     * @param url 头像链接
     */
    private updateHeadimg_blxf ( url: string ) {
        let image = wx.createImage();
        image.onload = () => {
            let texture = new cc.Texture2D();
            texture.initWithElement(image);
            texture.handleLoadedTexture();
            this.headimgSpr.spriteFrame = new cc.SpriteFrame(texture);
        };
        image.src = url;
    }
}
